from Selenium2Library.keywords.keywordgroup import KeywordGroup
from Selenium2Library.locators import ElementFinder
from selenium.common.exceptions import NoSuchElementException


class _BreezeTransferKeywords(KeywordGroup):
    """Library for Breeze Transfer Keywords purposes.
    
    This library is used in Breeze projects.
    """
    def __init__(self):
        """Does nothing. init only"""
        print '_BreezeTransferKeywords.__init__'
                    
            
    def validate_summary_screen_fields(self, transfer_Type, is_Repeat, from_Account):
        table_row_XPath = '//ul[@class="non-truncated striped spaced"]/li'
        
        #if (from_Account.startswith(('4','5')) == True):   
        #    from_Account = "CARD"
        if (len(from_Account) == 16):   
            from_Account = "CARD"        
        
        labelInfo = []
        labelValue = []
        returnValue = True
        
        labelInfo.append('Status')
        labelInfo.append('Receipt number')
        labelInfo.append('From')
        labelInfo.append('Amount')
        if is_Repeat != "True":
            labelInfo.append('Date')
        
        if is_Repeat == "True":
            labelInfo.append('Repeat')
            labelInfo.append('Frequency')
            labelInfo.append('Start Date')
            labelInfo.append('End Date')

        if transfer_Type == "OAT":
            labelInfo.append('To')
            
        if from_Account == "CARD":
            labelInfo.append('Available Cash Advance Limit')
            labelInfo.append('Bank Charges')
            labelInfo.append('Finance Charges')
            labelInfo.append('Transaction Type')            
             
        if transfer_Type == "THIRDPARTY":
            labelInfo.append('Payee')     
            labelInfo.append('Payee Account Number')
            labelInfo.append('Bank Name')
            labelInfo.append('Description')
            
        if transfer_Type == "THIRDPARTY_IBFT":
            labelInfo.append('Payee')     
            labelInfo.append('Payee Account Number')
            labelInfo.append('Bank Name')
            labelInfo.append('Description')

        if transfer_Type == "VMT":
            labelInfo.append('Payee')
            labelInfo.append('Payee Account Number')
            labelInfo.append('Description')
          
        labelInfo.sort()
        self._info(labelInfo)
        
        row = self._element_find(table_row_XPath, False, False)

        i = 1         

        for aRow in row:
            aRow_xpath = '//ul[@class="non-truncated striped spaced"]/li['+ str(i) +']/p[1]'
            labelValueText = str(aRow.find_element_by_xpath(aRow_xpath).text)
            labelValue.append(labelValueText)

            i = i + 1
        
        labelValue.sort()
        self._info(labelValue)
        
        if labelValue == labelInfo:
            returnValue = True
        else:
            returnValue = False
        
        return str(returnValue)
            
        
    def select_to_account(self, to_account, account_list_xpath):  
        ''' Return selected to account name and number by given account name.
        '''
        toAccountInfo = self._get_own_account_information(to_account, account_list_xpath)
        if toAccountInfo['hasAccount'] == True:
            toAccountInfo['accountLink'].click()

        return toAccountInfo['accountName']+'\n'+toAccountInfo['accountNumber']

    def select_from_account_pk(self, index):
        ''' Return a list of selected account name and account info by given currency.
        '''
        
        from_acct_xpath = "//div[@class='list-view']/dl/dd/ul/*"
        from_list = self._element_find(from_acct_xpath, False, False)
        from_acct_list = []
        i = 1
        
        for acct in from_list:
            account_name = acct.find_element_by_class_name("info-primary")
            account_number = acct.find_element_by_class_name("info-secondary")
            
            if i == int(index):
                # self._info("found %s %s" % (currency_cd, temp_currency))
                from_acct_info = (account_name.text + '\n' + account_number.text)
                # self._info("from_acct_info")
                # self._info(from_acct_info)
                from_acct_list.append(from_acct_info)
                from_acct_list.append(account_name.text)
                from_acct_list.append(account_number.text)
                acct.click()
                break
            else:
                i+=1
                
        return from_acct_list

    def select_to_account_in(self, index, typeOfTransfer):
        ''' Return selected account info by given currency.
        '''
        to_acct_xpath = "//*[@id='table-']/tbody/tr"
        acctName = './td/a/div/p[1]'
        acctNum= './td/a/div/p[2]'

        if typeOfTransfer == "OAT":
            acctBal= './td[2]/a'

        if typeOfTransfer == "THIRDPARTY":
            accountNumber = './td/a/p[3]'

        if typeOfTransfer == "SCBCARD":
            acctBal= './td[2]/a'

        if typeOfTransfer == "BILLPAY":
            to_acct_xpath = '//div[@id="holder-template"]/div[1]/div[2]/div[1]/ul/li/a'
            acctName = '//div[@id="holder-template"]/div[1]/div[2]/div[1]/ul/li/a/p[1]'
            acctNum = '//div[@id="holder-template"]/div[1]/div[2]/div[1]/ul/li/a/p[3]'

        to_acct_info = []
        
        
        #self._synchronize()
        self._info("BEGIN")
        elements = self._element_find(to_acct_xpath, False, False)
            
        i = 1
        
        for acct in elements:

            self._info(index)
            account_name = acct.find_element_by_xpath(acctName).text
            self._info(account_name)

            account_number = acct.find_element_by_xpath(acctNum).text
            self._info(account_number)
            
            if typeOfTransfer == "OAT":
                account_balance = acct.find_element_by_xpath(acctBal).text
                self._info(account_balance)
                
            if typeOfTransfer == "THIRDPARTY":
                account_number = acct.find_element_by_xpath(accountNumber).text
                self._info(account_number)

            if typeOfTransfer == "SCBCARD":
                account_balance = acct.find_element_by_xpath(acctBal).text
                self._info(account_balance)


            if i == int(index):
                self._info("fined")
                to_acct_info.append(account_name)
                to_acct_info.append(account_number)

                if typeOfTransfer == "OAT":
                    to_acct_info.append(account_balance)

                if typeOfTransfer == "THIRDPARTY":
                    to_acct_info.append(account_number)

                if typeOfTransfer == "SCBCARD":
                    to_acct_info.append(account_balance)

                if typeOfTransfer == "BILLPAY":
                    to_acct_info.append(account_name)

                acct.click()
                break
            else:
                i+=1
            
        return to_acct_info
        
    def select_to_account_sg(self, index, typeOfTransfer):
        ''' Return selected account info by given currency.
        '''
        to_acct_xpath = "//div[@id='holder-template']/div/div/div/table/tbody/tr"

        acctName = './td/a/p[1]'
        acctNum= './td/a/p[2]'
        if typeOfTransfer == "OAT":
            acctBal= './td[2]/a'
        if typeOfTransfer == "THIRDPARTY":
            accountNumber = './td/a/p[3]'
        
        if typeOfTransfer == "BILLPAY":
            to_acct_xpath = "//div[@id='holder-template']/div/div[2]/div/ul/li[1]"
            acctName = './a/p[1]'
            acctNum = './a/p[3]'
            
        to_acct_info = []
        
        
        #self._synchronize()
        self._info("BEGIN")
        elements = self._element_find(to_acct_xpath, False, False)
            
        i = 1
        
        for acct in elements:

            self._info(index)
            account_name = acct.find_element_by_xpath(acctName).text
            self._info(account_name)
            
            account_number = acct.find_element_by_xpath(acctNum).text
            self._info(account_number)
            
            if typeOfTransfer == "OAT":
                account_balance = acct.find_element_by_xpath(acctBal).text
                self._info(account_balance)
                
            if typeOfTransfer == "THIRDPARTY":
                account_number = acct.find_element_by_xpath(accountNumber).text
                self._info(account_number)

            if i == int(index):
                self._info("fined")
                to_acct_info.append(account_name)
                to_acct_info.append(account_number)
                if typeOfTransfer == "OAT":
                    to_acct_info.append(account_balance)
                if typeOfTransfer == "THIRDPARTY":
                    to_acct_info.append(account_number)

                if typeOfTransfer == "SCBCARD":
                    to_acct_info.append(account_name + ", " + account_number + ", " + "SGD")
                else:
                    to_acct_info.append(account_name + ", " + account_number)
                acct.click()
                break
            else:
                i+=1
            
        return to_acct_info
    
    def select_from_account_in(self, index):
        ''' Return selected account info by given currency.
        '''
        from_acct_xpath = "//*[@id='table-']/tbody/tr"

        acctName = './td[1]/a/div/p[1]'
        acctNum= './td[1]/a/div/p[2]'
        acctBal= './td[2]/a'
        
        from_acct_info = []
        
        #self._synchronize()
        self._info("BEGIN")
        elements = self._element_find(from_acct_xpath, False, False)
            
        i = 1
        
        for acct in elements:

            self._info(index)
            account_name = acct.find_element_by_xpath(acctName).text
            self._info(account_name)
            
            account_number = acct.find_element_by_xpath(acctNum).text
            self._info(account_number)

            if (account_number.startswith(('4','5')) == False):   
                account_balance = acct.find_element_by_xpath(acctBal).text
            else:
                account_balance = 0
                               
            if i == int(index):
                self._info("fined")
                from_acct_info.append(account_name)
                from_acct_info.append(account_number)
                from_acct_info.append(account_balance)
                acct.click()
                break
            else:
                i+=1
            
        return from_acct_info
    
    def select_from_account_sg(self, index, typeOfTransfer):
        ''' Return selected account info by given currency.
        '''
        from_acct_xpath = "//div[@id='holder-template']/div/div/div/table/tbody/tr"
        acctName = './td[1]/a/p[1]'
        acctNum= './td[1]/a/p[2]'
        acctBal= './td[2]/a'
        if typeOfTransfer == "BILLPAY":
            from_acct_xpath = "//*[@id='table-']/tbody/tr[3]"
            acctName = './td[1]/a/p[1]'
            acctNum = './td[1]/a/p[2]'
            acctBal= './td[2]/a'
        from_acct_info = []
        
        
        #self._synchronize()
        self._info("BEGIN")
        elements = self._element_find(from_acct_xpath, False, False)
            
        i = 1
        
        for acct in elements:

            self._info(index)
            account_name = acct.find_element_by_xpath(acctName).text
            self._info(account_name)
            
            account_number = acct.find_element_by_xpath(acctNum).text
            self._info(account_number)
            
            account_balance = acct.find_element_by_xpath(acctBal).text
            self._info(account_balance)
            
            if i == int(index):
                self._info("fined")
                from_acct_info.append(account_name)
                from_acct_info.append(account_number)
                from_acct_info.append(account_balance)
                if typeOfTransfer == "SCBCARD":
                    from_acct_info.append(account_name + ", " + account_number + ", " + "SGD")
                if typeOfTransfer == "BILLPAY":
                    from_acct_info.append(account_name + ", " + account_number + ", " + "SGD")
                else:
                    from_acct_info.append(account_name + ", " + account_number)
                acct.click()
                break
            else:
                i+=1
            
        return from_acct_info
    
    def select_to_account_pk(self, index, txn_type):
        ''' Return selected account info by given currency.
        '''
        to_acct_xpath = "//div[@id='main']/div[@id='content']/div/dl/*"
        amount_p = 'info-amount s-positive'
        amount_n = 'info-amount s-negative'
        acctName_cn = 'info-primary'
        acctNum_cn = 'info-secondary'
        acctNum_scb ='info-third'
        to_acct_info = []
        temp_list = ''
        
        #self._synchronize()
        self._info("BEGIN")
        elements = self._element_find(to_acct_xpath, False, False)
        self._info(elements)
        if txn_type == "OAT":
            temp_list = self._find_OAT_list(elements)

        if txn_type == "CARD":
            temp_list = self._find_CARD_list(elements)
            
        if txn_type == "SCB":
            temp_list = self._find_OtherAccount_list(elements)
            
        i = 1
        self._info(temp_list)
        for acct in temp_list:
            self._info(index)
            account_link = acct.find_element_by_class_name(acctName_cn).text
            self._info(account_link)
            if txn_type == "SCB":
                account_number =  acct.find_element_by_class_name(acctNum_scb).text   
            else:
                account_number = acct.find_element_by_class_name(acctNum_cn).text
            self._info(account_number)
            if i == int(index):
                self._info("fined")
                to_acct_info.append(account_link + '\n' + account_number)
                to_acct_info.append(account_number)
                acct.click()
                break
            else:
                i+=1
            
        return to_acct_info


        
    def _find_OAT_list(self, acct_list):
        flag = False
        acct_xpath = "//dd/ul/*"

        for element in acct_list:
            if flag == False:
                temp = element.find_element_by_id("OAT")
            if temp != None:
                if "My Accounts" == temp.text:
                    flag = True
                    temp = None
                    continue    
            if flag == True:
                oat_list = element.find_elements_by_xpath(acct_xpath)
                break
        
        return oat_list

    def _find_CARD_list(self, acct_list):
        flag = False
        acct_xpath = "//dd/ul/*"

        for element in acct_list:
            if flag == False:
                self._info("begin find card List")
                temp = element.find_element_by_xpath("//div[@id='main']/div[@id='content']/div/dl/dt[@id='CCP']")
                self._info(temp)
            if temp != None:
                if "My Cards" == temp.text:
                    flag = True
                    temp = None
                    continue    
            if flag == True:
                oat_list = element.find_elements_by_xpath(acct_xpath)
                self._info(oat_list)                                    
                break
        
        return oat_list

    def _find_OtherAccount_list(self, acct_list):
        flag = False
        acct_xpath = "//dd/ul/*"

        for element in acct_list:
            if flag == False:
                self._info("begin find SCB List")
                temp = element.find_element_by_xpath("//div[@id='main']/div[@id='content']/div/dl/dt[@id='IBFT']")
                self._info(temp)
            if temp != None:
                if "Other Accounts" == temp.text:
                    flag = True
                    temp = None
                    continue    
            if flag == True:
                oat_list = element.find_elements_by_xpath(acct_xpath)
                self._info(oat_list)                                    
                break
        
        return oat_list
    
    def get_to_account_balance(self, to_account, account_list_xpath):
        ''' Return selected to account balance by given account name. Must be used before select_to_account()
        '''
        toAccountInfo = self._get_own_account_information(to_account, account_list_xpath)
        return toAccountInfo['balance']
        
    def get_to_account_balance_IN(self, to_account_number, account_list):

        for i in xrange(len(account_list)):
            self._info("get account balance")
            self._info(i)
            self._info(account_list[i][1])
            self._info(to_account_number)
            if account_list[i][1] ==  to_account_number:
                self._info(account_list[i][3])
                return account_list[i][3]  
        
    def select_from_account(self, from_account, account_list_xpath):
        ''' Return selected from account name and number by given account name.
        '''
        fromAccountInfo = self._get_own_account_information(from_account, account_list_xpath)
        if fromAccountInfo['hasAccount'] == True:
            fromAccountInfo['accountLink'].click()

        return fromAccountInfo['accountName']+'\n'+fromAccountInfo['accountNumber']

    def get_from_account_name(self, from_account, account_list_xpath):
        ''' Return selected from account name by given account name. Must be used before select_from_account()
        '''
        fromAccountInfo = self._get_own_account_information(from_account, account_list_xpath)
        return fromAccountInfo['accountName']
    
    def get_from_account_balance(self, from_account, account_list_xpath):
        ''' Return selected from account balance by given account name. Must be used before select_from_account()
        '''
        fromAccountInfo = self._get_own_account_information(from_account, account_list_xpath)
        return fromAccountInfo['balance']
    def get_from_account_balance_PK(self, from_account_number, account_list):

        for i in xrange(len(account_list)):
            self._info("get account balance")
            self._info(i)
            self._info(account_list[i][1])
            self._info(from_account_number)
            if account_list[i][1] ==  from_account_number:
                self._info(account_list[i][3])
                return account_list[i][3]
                
    def get_from_account_balance_IN(self, from_account_number, account_list):

        for i in xrange(len(account_list)):
            self._info("get account balance")
            self._info("Printing accountlist array")
            self._info(account_list[i])
            self._info("Printing accountlist 1st object")
            self._info(account_list[i][1])
            self._info("Printing account number")

            if account_list[i][1] ==  from_account_number:
                self._info("account number match")
                self._info(account_list[i][3])
                return account_list[i][3]            
        
    def _get_own_account_information(self, account_selected, account_list_xpath):
        ''' Return to account information by given account name.
        '''
        accountList = self._element_find(account_list_xpath, False, False)
        accountInfo = {'accountLink':'','accountName':'','accountNumber':'','balance':'','hasAccount':False}
        for account in accountList:
            accountName = account.find_element_by_class_name('info-primary').text
            accountNumber = account.find_element_by_class_name('info-secondary').text
            balance = account.find_element_by_class_name('info-amount').text
            if account_selected in accountName.strip():
                self._info("found %s %s %s" % (accountName, accountNumber, balance))
                accountInfo['accountLink'] = account
                accountInfo['accountName'] = accountName
                accountInfo['accountNumber'] = accountNumber
                accountInfo['balance'] = balance
                accountInfo['hasAccount'] = True
                break
        
        return accountInfo

    
    def _synchronize(self, sync_ui=True, sync_ws=True, nodelay=False):
        """Wait until page is ready, i.e. application finished rendering and webservice requests are completed
        """
        self._debug("Entering to synchronize")
        if not nodelay:
            time.sleep(3)
        maxtries = 60
        while maxtries:
            maxtries -= 1
            UIisRendering, WSinProgress = self.execute_javascript(" try { return [ BreezeUIisRendering, BreezeWSinProgress ]; } catch (e) { return [ true, false ] } ")
            ui_ok = (not UIisRendering) if sync_ui else True
            ws_ok = (not WSinProgress) if sync_ws else True
            if ui_ok and ws_ok:
                self._debug("Leaving synchronize with %d tries left" % maxtries)
                return
            time.sleep(1.0)
        self._debug("Leaving synchronize by timeout")

    def get_transfer_date(self):
        ''' Used in make-transfer.tsv to get transafer date.
        '''
        return self._run('get_transfer_date')
            
    def _get_transfer_date_pk(self):
        date_list = []
        transfer_date_xpath = "//*[@id='TR_DT']"
        browser = self._current_browser()
        transfer_date = browser.find_element_by_xpath(transfer_date_xpath)
        date_temp = []

        date = transfer_date.text
        self._info(date)
        date = date.split("\n")[1]
        self._info(date)
        date_temp = date.split(',')
        date_list.append(date)
        date_list.append(date_temp[1].strip())
        
        return date_list

    def _get_transfer_date_hk(self):
        date_list = []
        transfer_date_xpath = "//*[@id='TR_DT']"
        browser = self._current_browser()
        transfer_date = browser.find_element_by_xpath(transfer_date_xpath)
        date_temp = []

        date = transfer_date.text
        self._info(date)
        date = date.split("\n")[1]
        self._info(date)
        date_temp = date.split(',')
        date_list.append(date)
        date_list.append(date_temp[1].strip())
        
        return date_list
    
    def _get_transfer_date_id(self):
        date_list = []
        transfer_date_xpath = "//*[@id='TR_DT']"
        browser = self._current_browser()
        transfer_date = browser.find_element_by_xpath(transfer_date_xpath)
        date_temp = []

        date = transfer_date.text
        self._info(date)
        date_temp = date.split(',')
        date_list.append(date)
        date_list.append(date_temp[1].strip())
        
        return date_list
    
    def _find_lastest_transaction_history(self):
        transfer_history_xpath = "//div[@class='list-view']/dl/*"
        browser = self._current_browser()
        history_list = self._element_finder.find(browser, transfer_history_xpath)
        today_flag = False
        result_his = history_list[0]

        for his in history_list:
            today_history = his.find_element_by_class_name("info-primary")
            today_his = today_history.text
            self._info("history text:" + today_his)
                
            if today_his == 'Today':
                today_flag = True
                continue

            if today_flag == True:
                self._info("find break here and end this function")
                result_his = his
                break

        return result_his
    
    def _find_today_specified_transaction_history(self, transfer_type, account_name, amount_value):
        self._info(transfer_type)
        self._info(account_name)
        self._info(amount_value)
        transfer_history_xpath = "//div[@class='list-view']/dl/*"
        browser = self._current_browser()
        history_list = self._element_finder.find(browser, transfer_history_xpath)
        today_flag = False
        result_his = history_list[0]
        
        for his in history_list:
            today_history = his.find_element_by_class_name("info-primary")
            today_his = today_history.text
            self._info("history text:" + today_his)
                
            if today_his == 'Today':
                today_flag = True
                continue
            
            if today_his == 'Yesterday':
                today_flag = False
                self._info("Not Found the specified record in today's transfer history.")
                break

            if today_flag == True:
                type = his.find_element_by_class_name("info-primary").text.strip()
                name = his.find_element_by_class_name("info-secondary").text.strip()
                value = his.find_element_by_class_name("info-amount").text.strip()
                diff_value = self.get_transfer_amount(value, amount_value)
                if type==transfer_type and name==account_name and diff_value=='0.0' :
                    self._info("Find break here and end this function")
                    result_his = his
                    break

        return result_his

    def get_from_account_transfer_history(self, transfer_type, account_name, amount_value):
        ''' Used in make-transfer.tsv to get tfrom account transfer history.
        
        In keyword of Verify Transfer History
        '''
        today_his_list = []
        #today_his = self._find_lastest_transaction_history()
        today_his = self._find_today_specified_transaction_history(transfer_type, account_name, amount_value)
        from_acct = today_his.find_element_by_class_name("info-secondary").text
        self._info(from_acct)
        today_his_list.append(from_acct)
        today_amount = today_his.find_element_by_class_name("info-amount").text
        self._info(today_amount)
        today_his_list.append(today_amount)
        return today_his_list

    def get_from_account_transfer_history_by_name(self,from_account_name,from_amount,txn_type):
        ''' Used in make-transfer.tsv to get tfrom account transfer history.
                In keyword of focus Account Name and Amount
        '''
        
        transfer_history_xpath = "//div[@class='list-view']/dl/*" 
        '''transfer_history_xpath = "//table[@class='width-type-8 l-r trans-list']/tbody/tr/td/a/p"'''
        browser = self._current_browser()
        history_list = self._element_finder.find(browser, transfer_history_xpath)
        self._info(history_list)
        today_flag = False
        exist_flag = False
        result_his = history_list[0]
        current_list=[]
        TXN_TYPE_HIS =""
        
        if txn_type == "OAT":
           TXN_TYPE_HIS = 'Transfer Between Own Accounts'
           
        if txn_type == "CARD":
           TXN_TYPE_HIS = 'Card Payment'
           
        if txn_type == "SCB":
           TXN_TYPE_HIS = 'Local Transfer (Within the Bank)'
           
        if txn_type == "IBFT":
           TXN_TYPE_HIS = 'Local Transfer (Outside the Bank)'
           
        for his in history_list:
            today_history = his.find_element_by_class_name("info-primary")
            today_his = today_history.text
            self._info("history text:" + today_his)
                
            if today_his == 'Today':
                today_flag = True
                continue
            if today_his == 'Yesterday':
                today_flag = False
                break
            if today_flag == True and today_his == TXN_TYPE_HIS:
                self._info("find !!! and continue this function")
                current_list.append(his)
                continue

            
        for his in current_list:
            current_from_account = his.find_element_by_class_name("info-secondary")
            current_account = current_from_account.text
            self._info("current ACCOUNT:" + current_account)
            self._info("FROM ACCOUNT:" + from_account_name)
            current_amount = his.find_element_by_class_name("info-amount").text
            self._info("current AMOUNT:" + current_amount)
            self._info("FROM AMOUNT:" +'PKR'+from_amount)
            if current_account == from_account_name and current_amount=='PKR'+from_amount:
                self._info("FINED")
                exist_flag = True
                transfer_type = his.find_element_by_class_name("info-primary")
                transfer_type.click()
                return exist_flag
            
        return exist_flag
     
    def get_from_account_transfer_history_in(self,from_account_name,from_amount,txn_type):
        ''' Used in make-transfer.tsv to get tfrom account transfer history.
                In keyword of focus Account Name and Amount
        '''

        transfer_history_xpath = "//table[@class='width-type-8 l-r trans-list']/tbody/tr" 

        '''transfer_history_xpath = "//table[@class='width-type-8 l-r trans-list']/tbody/tr/td/a/p"'''
        browser = self._current_browser()
        history_list = self._element_finder.find(browser, transfer_history_xpath)
        self._info(history_list)
        today_flag = False
        exist_flag = False
        result_his = history_list[0]
        current_list=[]
        TXN_TYPE_HIS =""
        
        if txn_type == "OAT":
           TXN_TYPE_HIS = 'Transfer Between Own Accounts'
           
        if txn_type == "CARD":
           TXN_TYPE_HIS = 'Card Payment'
           
        if txn_type == "SCB":
           TXN_TYPE_HIS = 'Local Transfer (Within the Bank)'
           
        if txn_type == "IBFT":
           TXN_TYPE_HIS = 'Local Transfer (Outside the Bank)'
           
        for his in history_list:
            today_history = his.find_element_by_class_name("info-primary")
            today_his = today_history.text
            self._info("history text:" + today_his)
                
            if today_his == 'Today':
                today_flag = True
                continue
            if today_his == 'Yesterday':
                today_flag = False
                break
            if today_flag == True and today_his == TXN_TYPE_HIS:
                self._info("find !!! and continue this function")
                current_list.append(his)
                continue

            
        for his in current_list:
            current_from_account = his.find_element_by_class_name("info-secondary")
            current_account = current_from_account.text
            self._info("current ACCOUNT:" + current_account)
            self._info("FROM ACCOUNT:" + from_account_name)
            current_amount = his.find_element_by_class_name("info-amount").text
            self._info("current AMOUNT:" + current_amount)
            self._info("FROM AMOUNT:" +'PKR'+from_amount)
            if current_account == from_account_name and current_amount=='PKR'+from_amount:
                self._info("FINED")
                exist_flag = True
                transfer_type = his.find_element_by_class_name("info-primary")
                transfer_type.click()
                return exist_flag
            
        return exist_flag
     

        
    def click_current_transfer_history(self, transfer_type, account_name, amount_value):
        ''' Used in make-transfer.tsv to get current transfered transaction history.
        
        In keyword of Verify Transfer History Detail
        '''
        #today_his = self._find_lastest_transaction_history()
        today_his = self._find_today_specified_transaction_history(transfer_type, account_name, amount_value)
        transfer_type = today_his.find_element_by_class_name("info-primary")
        transfer_type.click()

    def click_current_transfer_history_by_name(self,from_account_name,from_amount):
        ''' Used in make-transfer.tsv to get current transfered transaction history.
        
        In keyword of Verify Transfer History Detail
        '''
        transfer_history_xpath = "//div[@class='list-view']/dl/*"
        browser = self._current_browser()
        history_list = self._element_finder.find(browser, transfer_history_xpath)
        today_flag = False
        exist_flag = False
        result_his = history_list[0]
        current_list=[]
        for his in history_list:
            today_history = his.find_element_by_class_name("info-primary")
            today_his = today_history.text
            self._info("history text:" + today_his)
                
            if today_his == 'Today':
                today_flag = True
                continue
            if today_his == 'Yesterday':
                today_flag = False
                break
            if today_flag == True and today_his=='Between Own Accounts':
                self._info("find break here and end this function")
                current_list.append(his)
                continue

            
        for his in current_list:
            current_from_account = his.find_element_by_class_name("info-secondary")
            current_account = current_from_account.text
            self._info("current:" + current_account)
            current_amount = his.find_element_by_class_name("info-amount").text
            self._info("current_amount:" + current_amount)    
            if current_account == from_account_name:
                exist_flag = True
                today_his_but=his
                break
        transfer_type = today_his_but.find_element_by_class_name("info-primary")
        transfer_type.click()

    def select_to_others_account(self, bankname):
        ''' Return selected account info by given bankname.
        '''
        element = self._element_find("//*[@id='IBFT']/a", True, False)
        element.click()
        self.wait_until_page_contains_element("//dt[@id='IBFT']/following-sibling::dd/ul/li")

        other_acct_xpath = "//dt[@id='IBFT']/following-sibling::dd/ul/li"
        ibft_list = self._element_find(other_acct_xpath, False, False)

        to_acct_info = ''
        for acct in ibft_list:
            account = acct.find_element_by_xpath("./a/span[1]")
            temp_bankname = acct.find_element_by_xpath("./a/span[2]")
            acct_number = acct.find_element_by_xpath("./a/span[3]")

            if bankname in temp_bankname.text.strip():
                self._info("found %s %s" % (bankname, temp_bankname.text))
                to_acct_info = (account.text + '\n' + acct_number.text)
                acct.click()
                break

        return to_acct_info

    def get_transfer_amount(self,stringA,stringB):
        ''' Return two strings minus number
            By Carl
        ''' 
        a=''
        b=''
        if len(stringA)>16 or len(stringB)>16:
			stringA = stringA[-16:]
			stringB = stringB[-16:]
		
        for i in stringA:
            if ord(i)>=46 and ord(i)<=57:
                a=a+i
                
        for i in stringB:
            if ord(i)>=46 and ord(i)<=57:
                b=b+i
        
        result=round(float(a)-float(b),2)
        
        
        return str(result)

    def get_amount_withoutCurrencyCode(self,amountString):
        '''return amount without currency code
        '''
        retString = ""
        for eachLetter in amountString:
            if eachLetter.isdigit():
                retString += eachLetter
            elif eachLetter == '.':
                retString += eachLetter

        self._info (retString)
        return retString